package com.example.imbmq80ai.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsOperations;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

import javax.jms.Message;
import javax.jms.TextMessage;

/**
 * @program: emp_trunk
 * @description: 监听MQ的相应队列
 * @author: Xiaos
 * @create: 2020-10-14 10:21
 **/
@Slf4j
@Component
public class MQListener extends MessageListenerAdapter {
    @Autowired
    JmsOperations jmsOperations;

    @Autowired
    private JmsTemplate jmsTemplate;

    @Override
    @JmsListener(destination = "${project.mq.queuename}", selector = "JMSCorrelationID='1111'")
    public void onMessage(Message message) {
        log.info("从MQ接收的message报文:"+message);


        jmsTemplate.send("QUEUE1", session -> {
            TextMessage m = session.createTextMessage();
            m.setText("return data");
            m.setJMSCorrelationID("2222");

            return m;
        });
    }

}

